﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using AdminModule.IndexMarkServiceReference;
using System.Windows.Data;
using AdminModule.AnswerLog.DataSource;
using System.Collections.ObjectModel;
using AdminModule.CommonClasses;

namespace AdminModule.Views
{
    public partial class ViewAnswerLogDetails : ChildWindow
    {
        public bool AnswerLogChanged = false;
        private IndexMarkServiceClient client = new IndexMarkServiceClient();
        private ObservableCollection<DTOBase> SelectedItems = new ObservableCollection<DTOBase>();
        private int IDAnswer = 0;

        public ViewAnswerLogDetails(int IDAnswer)
        {
            InitializeComponent();
            this.IDAnswer = IDAnswer;
            SelectedItems.CollectionChanged += new System.Collections.Specialized.NotifyCollectionChangedEventHandler(SelectedItems_CollectionChanged);
            client.GetAnswerLogDetailsCompleted += new EventHandler<GetAnswerLogDetailsCompletedEventArgs>(client_GetAnswerLogDetailsCompleted);
            client.SaveAnswerLogCompleted += new EventHandler<System.ComponentModel.AsyncCompletedEventArgs>(client_SaveAnswerLogCompleted);
        }

        void SelectedItems_CollectionChanged(object sender, System.Collections.Specialized.NotifyCollectionChangedEventArgs e)
        {
            deleteButton.IsEnabled = SelectedItems.Count > 0;
        }

        void client_SaveAnswerLogCompleted(object sender, System.ComponentModel.AsyncCompletedEventArgs e)
        {
            if (e.Error == null)
            {
                // Показываем прогресс-бар и заново загружаем записи
                progressBar.IsVisibility = System.Windows.Visibility.Visible;
                AnswerLogChanged = true;
                SelectedItems.Clear();
                client.GetAnswerLogDetailsAsync(IDAnswer);
            }
            else
            {
                progressBar.IsVisibility = System.Windows.Visibility.Collapsed;
                MessageBox.Show(e.Error.Message);
            }
        }

        void client_GetAnswerLogDetailsCompleted(object sender, GetAnswerLogDetailsCompletedEventArgs e)
        {
            if (e.Result.Count > 0)
            {
                Title = string.Format("История ввода: {0} - {1}", e.Result.First().Characteristic.Name, e.Result.First().Respondent.Person.LastName);
                dataGrid.ItemsSource = new PagedCollectionView(new GridTreeDataSource(e.Result, item => (item as AdmAnswerLog).Childs).Source);
            }
            progressBar.IsVisibility = System.Windows.Visibility.Collapsed;
        }

        private void OKButton_Click(object sender, RoutedEventArgs e)
        {
            this.DialogResult = true;
        }

        private void CancelButton_Click(object sender, RoutedEventArgs e)
        {
            this.DialogResult = false;
        }

        private void ChildWindow_Loaded(object sender, RoutedEventArgs e)
        {
            progressBar.IsVisibility = System.Windows.Visibility.Visible;
            client.GetAnswerLogDetailsAsync(IDAnswer);
        }

        /// <summary>
        /// Отметить запись
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void CheckBox_Checked(object sender, RoutedEventArgs e)
        {
            CheckBox checkBox = sender as CheckBox;
            DTOBase item = null;

            if (checkBox.Tag is GridTreeDataItem)
            {
                GridTreeDataItem dataItem = checkBox.Tag as GridTreeDataItem;
                item = dataItem.Item as DTOBase;
            }
            else
            {
                item = checkBox.Tag as DTOBase;
            }

            if (checkBox.IsChecked == true)
            {
                if (!this.SelectedItems.Contains(item))
                {
                    this.SelectedItems.Add(item);
                }
            }
            else
            {
                this.SelectedItems.Remove(item);
            }
        }

        private void deleteButton_Click(object sender, RoutedEventArgs e)
        {
            MessageBoxResult res = MessageBox.Show("Удалить?", MyAssemblyInfo.Title, MessageBoxButton.OKCancel);
            if (res == MessageBoxResult.OK)
            {
                progressBar.IsVisibility = System.Windows.Visibility.Visible;
                client.SaveAnswerLogAsync(SelectedItems);
            }
        }
    }
}

